#include "MainHeader.h"

	clsGameClass::clsGameClass(){
		 ZMHeader=0x45584D42;
		 this->fGbuf.resize(0x8000);
		 PcPalMem.resize(0x200);
		 strMovement.resize(256);
	}
	clsGameClass::~clsGameClass(){}

	int clsGameClass::Setup(){
		int i=0;
		if(Game==0){
			WorkArea.cboArea.Clear();
		    WorkArea.cboArea.Additem("Brinstar");
            WorkArea.cboArea.Additem("Kraid");
			WorkArea.cboArea.Additem("Norfair");
			WorkArea.cboArea.Additem("Ridley");
			WorkArea.cboArea.Additem("Tourian");
			WorkArea.cboArea.Additem("Crateria");
			WorkArea.cboArea.Additem("Chozodia");
            WorkArea.cboArea.SetListIndex(0);

            RoomTilesets.resize(0x50);//ZM has 0x50 Tilesets
            RoomOffsets.resize(8);

	      fseek(GBA.ROM, 0x33DFDC,SEEK_SET);//Tileset
		  GBA.Seek(0x33DFDC);
		  for( i=0;i<RoomTilesets.size();i++){
				GBA.Read(4,&RoomTilesets[i].gTiles);
				GBA.Read(4,&RoomTilesets[i].pTiles);
				GBA.Read(4,&RoomTilesets[i].gBackground);
				GBA.Read(4,&RoomTilesets[i].TSAMap);
				GBA.Read(4,&RoomTilesets[i].EffectSet);
   			}
			
			
			GBA.Seek(0x75FAC4);

            for( i=0;i<RoomOffsets.size();i++){
				GBA.Read(4,&RoomOffsets[i]);
				
   			}
			LoadClipData();


		}



    return 0;
	}
int FindEnd(char* src){
  if(src[0]=='E' && src[1] == 'N' && src[2]=='D') return 1;
  return 0;

}
int clsGameClass::LoadClipData(){
char MoveDataPath[1024];
int temp=0;
int max=0;
int i=0;
char strTmp[1024]={0};
char cs[128]={0};
if(Game==0){
   sprintf(MoveDataPath, "%s\\ZM\\clip.txt", AppPath);
}
FILE* fp=fopen(MoveDataPath,"r");
if(fp){
	fseek(fp,0,SEEK_SET);
	while(1){
	 fgets(cs, 128, fp);
	 if(FindEnd(cs)==1){
		 break;
	 }
	 sscanf(cs, "%X", &temp);
	 strcpy(strTmp,&cs[5]);//hack
	 strMovement[temp] = strTmp;
	 if(temp>max) max=temp;
	}

}
if(fp)fclose(fp);
for(i=0;i<255;i++){
    
	if(strMovement[i].size()){
		sprintf(strTmp,"%02X %s",i,strMovement[i].c_str());
	}else{
		
  
       sprintf(strTmp,"%02X Unknown",i);

	}
   WorkArea.cboClipdata.Additem(strTmp);
}
WorkArea.cboClipdata.SetListIndex(0);

return 0;
}
int clsGameClass::LoadHeader(){
    curRoomOffset= RoomOffsets[curArea]+(curRoom*0x3C)-0x8000000;
	//We'll do some invalid testing here I guess.
	GBA.Seek(curRoomOffset);

	fread(&RoomHeader.bTileset,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bBg0,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bBg1,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bBg2,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.lBg3,sizeof(u32),1,GBA.ROM);
	fread(&RoomHeader.lForeground,sizeof(u32),1,GBA.ROM); 
	if(RoomHeader.lForeground<0x8000000) return -1;
	fread(&RoomHeader.lLevelData,sizeof(u32),1,GBA.ROM);
	if(RoomHeader.lLevelData<0x8000000) return -1;
	fread(&RoomHeader.lBackLayer,sizeof(u32),1,GBA.ROM);
	if(RoomHeader.lBackLayer<0x8000000) return -1;
	fread(&RoomHeader.lClipData,sizeof(u32),1,GBA.ROM); 
	if(RoomHeader.lClipData<0x8000000) return -1;
	fread(&RoomHeader.lBackgroundTSA,sizeof(u32),1,GBA.ROM);
	if(RoomHeader.lBackgroundTSA<0x8000000) return -1;
	fread(&RoomHeader.bUnknown1,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.TransForeground,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.iSep1,sizeof(u16),1,GBA.ROM);
	fread(&RoomHeader.lSpritePointer,sizeof(u32),1,GBA.ROM); 
	if(RoomHeader.lSpritePointer<0x8000000) return -1;
	fread(&RoomHeader.bSpriteIndex1,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bEventSwitch,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.iSep2,sizeof(u16),1,GBA.ROM);
	fread(&RoomHeader.lSpritePointer2,sizeof(u32),1,GBA.ROM);
	if(RoomHeader.lSpritePointer2<0x8000000) return -1;
	fread(&RoomHeader.bSpriteIndex2,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bEventSwitch2,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.iSeperator,sizeof(u16),1,GBA.ROM);
	fread(&RoomHeader.lSpritePointer3,sizeof(u32),1,GBA.ROM); 
	if(RoomHeader.lSpritePointer3<0x8000000) return -1;
	fread(&RoomHeader.bSpriteIndex3,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bMiniMapRoomX,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bMiniMapRoomY,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bEffect,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bSceneryYPos,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bNothing,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.bMusic,sizeof(u8),1,GBA.ROM);
	fread(&RoomHeader.blank,sizeof(u8),1,GBA.ROM);

    
     return curRoomOffset;

}

int clsGameClass::LoadSpecialEffects(long EffectNum){

long  reference_table,tileset_table,ref_offset=0;
long  ani_source,gfx_destination,i,pointer=0;
 

u8 ref_array[0x30];
u8 Graphic[128];
 
 

if(Game == 0){
 reference_table = 0x35FA78;
 tileset_table = 0x35F948;
}else if(Game ==1){
 reference_table = 0x3C8F90;
 tileset_table =0x3C8D58;
}


fseek(GBA.ROM,reference_table + ((EffectNum&0xFF) * 0x30),SEEK_SET);
fread(&ref_array,sizeof(u8),0x30,GBA.ROM);

 for(i = 0; i < 16;i++){


	ani_source = (tileset_table + (8 * ref_array[i * 3]));
    fseek(GBA.ROM,ani_source,SEEK_SET);
	fread(&SpecialEffects[i].Animated,sizeof(u8),1,GBA.ROM);
	fread(&SpecialEffects[i].Timer,sizeof(u8),1,GBA.ROM);
	fread(&SpecialEffects[i].GraphicEnd,sizeof(u16),1,GBA.ROM);
	fread(&SpecialEffects[i].GraphicPnt,sizeof(u32),1,GBA.ROM);
	gfx_destination = i *0x80; //if 0 is bufferstart...
	if(SpecialEffects[i].GraphicPnt<0x7FFFFFF) continue;
	fseek(GBA.ROM,SpecialEffects[i].GraphicPnt - 0x8000000,SEEK_SET);

	fread(&Graphic[0],sizeof(u8),128,GBA.ROM);
	for (int ii = 0; ii < 128;ii++)	fGbuf[gfx_destination + ii] = Graphic[ii];
 
 }

return 0;
}

int clsGameClass::ZMGlobalGFX(){

if( curArea == 6){
   
   fseek(GBA.ROM,0x5DA40C,SEEK_SET);
   fread(&fGbuf[2048],1,4098,GBA.ROM);
 
   
}else{
  
   fseek(GBA.ROM,0x5D940C,SEEK_SET);
   fread(&fGbuf[0x800],1,4098,GBA.ROM);

}
	


return 0;
}


int clsGameClass::MFGlobalGFX(){




GBA.GBAPal[0] = 0;
fseek(GBA.ROM,0x40805E,SEEK_SET);
fread(&GBA.GBAPal[1],2,47,GBA.ROM);

fseek(GBA.ROM,0x3F28C8,SEEK_SET);
fread(&fGbuf[0x800],1,4098,GBA.ROM);

return 0;

}
int clsGameClass::GetTileSet(){
	int Tileset=RoomHeader.bTileset;
    unsigned char decompbuf[84100]={0};
	long decomplength=0;
    //long Tileset=0;
	u16  TileCheck=0;
    int  i =0;
  
    //Tileset = Combos[1].GetListIndex();

	fseek(GBA.ROM,RoomTilesets[Tileset].gTiles-0x8000000,SEEK_SET);//Seek the compressed Tileset
	fread(&GBA.CompBuffer,sizeof(char),64691,GBA.ROM);//Read compression
	fseek(GBA.ROM,RoomTilesets[Tileset].pTiles-0x8000000,SEEK_SET);//Seek the endcoded pal
	fread(&GBA.GBAPal[32],sizeof(short),0x1E0,GBA.ROM);//Read pal
	fseek(GBA.ROM,RoomTilesets[Tileset].TSAMap-0x8000000,SEEK_SET);

	 
	
	memset(&TSA.nTSA,0,sizeof(TSA.nTSA));
	fread(&TSA.ID,sizeof(short),1,GBA.ROM); 
	TSA.max=0;
	//fread(&TSA.nTSA,sizeof(short),0x1080,GBA.ROM);
    for(i = 0;;i++){//Infinte 
           fread(&TileCheck,2,1,GBA.ROM); 
		  		  if(TileCheck==0x2)
					break;
		  	TSA.nTSA[i]=TileCheck;
			TSA.max++;
	}
    TSA.max/=4;

	GBA.GBAPal[0]=0;
	GBA.GBAPal[16] = 0;
	
	
	if(Game == 0){
	fseek(GBA.ROM,0x31C72E,SEEK_SET);
	fread(&GBA.GBAPal[1],sizeof(short),15,GBA.ROM);
    fseek(GBA.ROM,0x3ED51C,SEEK_SET);
	fread(&GBA.GBAPal[16],sizeof(short),16,GBA.ROM);
	

		if( curArea == 6){
			fseek(GBA.ROM,0x5DA40C,SEEK_SET);
			fread(&fGbuf[2048],1,4098,GBA.ROM);   
		}else{
            fseek(GBA.ROM,0x5D940C,SEEK_SET);
			fread(&fGbuf[0x800],1,4098,GBA.ROM);
		}
	}
	/*}else if(Game == 1){
        GBA.GBAPal[0] = 0;
		fseek(GBA.ROM,0x40805E,SEEK_SET);
		fread(&GBA.GBAPal[1],2,47,GBA.ROM);
		fseek(GBA.ROM,0x3F28C8,SEEK_SET);	
		fread(&fGbuf[0x800],1,4098,GBA.ROM);
     }*/
    GBA.DecodePal(GBA.GBAPal, &PcPalMem[0], 16, 0);

 

	   
    
	 
	long size =GBA.LZ77UnComp(GBA.CompBuffer, decompbuf);
		 
	//memcpy(fGbuf[6144],decompbuf[0],size);
   for( i = 0;i<size;i++)fGbuf[6144+i]=decompbuf[i];
   LoadSpecialEffects(RoomTilesets[Tileset].EffectSet);
   
  

	TileImage.Load(&fGbuf[0],1024);

   // DrawTileset();

	







	return 0;

}

int clsGameClass::LoadRoom(){

    if(RoomHeader.bBg0) GetLayer(RoomHeader.bBg0, 0, RoomHeader.lForeground-0x8000000);
	if(RoomHeader.bBg1) GetLayer(RoomHeader.bBg1, 1, RoomHeader.lLevelData-0x8000000);
	if(RoomHeader.bBg2) GetLayer(RoomHeader.bBg2, 2, RoomHeader.lBackLayer-0x8000000);
	GetLayer(0x10, 4, RoomHeader.lClipData-0x8000000); 

	return 0;
}
	
int  clsGameClass::GetLayer(u8 compression, u8 Layer, u32 offset){
nRoomBuff* ELayer[5]={	&ThisForeground,&ThisLevelData,&ThisBackLayer,&ThisBackground,&ThisClipData};//Edit Layer
fseek(GBA.ROM,offset,SEEK_SET);	
	if( compression == 0x10){
		fread(&ELayer[Layer]->X,sizeof(u8),1,GBA.ROM);		//ThisLevelData.X++;		// DISCH -- added inc
		fread(&ELayer[Layer]->Y,sizeof(u8),1,GBA.ROM);		//ThisLevelData.Y++;		// DISCH -- added inc
		// sprintf(buffer,"X is %x Y is %x",ThisLevelData.X,ThisLevelData.Y);
		// sMessage(buffer);
		fread(GBA.CompBuffer,sizeof(u8),64691,GBA.ROM);
		
		//GBA.ZMUncompRle(GBA.CompBuffer, (u8*)ThisLevelData.TileBuf2D ,ThisLevelData.X*ThisLevelData.Y, 1);
		ELayer[Layer]->BImage.Create(ELayer[Layer]->X*16,ELayer[Layer]->Y*16);
		ELayer[Layer]->BImage.SetPalette(&PcPalMem[0]);
		memset(ELayer[Layer]->TileBuf2D,0,0x20000);
		ELayer[Layer]->oSize=GBA.ZMUncompRle(ELayer[Layer]->X*ELayer[Layer]->Y, GBA.CompBuffer, (u8*)&ELayer[Layer]->TileBuf2D);
	}else if( compression==0x40){
        fseek(GBA.ROM,offset,SEEK_SET);	
		ELayer[Layer]->size=fgetc(GBA.ROM);
		
			if((ELayer[Layer]->size == 1)||(ELayer[Layer]->size >= 3)) {
				ELayer[Layer]->X = 64;
				ELayer[Layer]->Y = 32;
			}else if(ThisBackground.size == 2){
				ELayer[Layer]->X = 32;
				ELayer[Layer]->Y = 64;
			}else{
				
				ELayer[Layer]->X = 32; 
				ELayer[Layer]->Y = 32;
			}


		fseek(GBA.ROM,offset+4,SEEK_SET);
		fread(&GBA.CompBuffer,sizeof(u8),32691,GBA.ROM);
		memset(ELayer[Layer]->TileBuf2D,0,0x20000);
		//ThisBackLayer.oSize=GBA.LZ77UnComp(GBA.CompBuffer, decompbuf);
        ELayer[Layer]->oSize=GBA.LZ77UnComp(GBA.CompBuffer, (u8*)ELayer[Layer]->TileBuf2D);
		ELayer[Layer]->BImage.Create(1024,1024);
		ELayer[Layer]->BImage.SetPalette(&PcPalMem[0]);
		//memcpy(ELayer[Layer]->TileBuf2D,decompbuf,ELayer[Layer]->oSize);
	}
return 0;
}
int clsGameClass::FullLoad(){
clsGame.LoadHeader();
clsGame.GetTileSet();
Tools.DrawTileset();
clsGame.LoadRoom();
clsGame.DrawAll=1;
clsGame.DrawRoom();
return 0;


}
int clsGameClass::DrawLayer(nRoomBuff* Map, u8 ctype){//img is the image to blit to 
int thisX=0,thisY=0,mX=0,mY=0;
u16 TILE = 0;
int i =0;
int X=Map->X;
int Y=Map->Y;
int ThisY=0;
int ThisX=0;
u16* TileBuf2D=Map->TileBuf2D;
Image* pic=&Map->BImage;


pic->SetPalette(&PcPalMem[0]);

	
	//	for(thisY=Map->DrawRect.top;thisY<(Map->DrawRect.bottom);thisY++){

	//		for(thisX = Map->DrawRect.left; thisX < (Map->DrawRect.right);thisX++){// from here if something is enabled then draw it 
		for(thisY=0;thisY<(Map->Y);thisY++){

			for(thisX=0;thisX<(Map->X);thisX++){// from here if something is enabled then draw it 
						TILE =(TileBuf2D[thisX+(thisY * X)]);
						BitBlt(pic->DC(),(thisX)<<4, (thisY)<<4,16,16,imgTileset.DC(),(TILE%16)<<4,(TILE>>4)<<4,SRCCOPY);
					
	   
				}
			}
		
		//Rect to draw

	
   


	return 0;
}

int clsGameClass::DrawRoom(){

	u8 cSize=0;
	u8 fd=0;
	u32 fWidth=0,fHeight=0;
	//int Width=img[1].Width,Height=img[1].Height;
	int k =0;

	u16 Width=ThisLevelData.X<<4, Height=ThisLevelData.Y<<4;
	if(!((Game == 0)||(Game==1))) return 0;
	if(DrawAll==1){


    ThisForeground.Dirty =1 ;
	ThisLevelData.Dirty =1;
	ThisBackLayer.Dirty = 1;
	DrawAll=0;
	}

	imgMap.Create(Width,Width);
	
	bbMap.Create(Width, Height);
	SetBkMode(bbMap.DC(), TRANSPARENT);
	SetTextColor(bbMap.DC(),RGB(255,255,255));
	imgMap.SetPalette(&PcPalMem[0]);
	
 	
	TileImage.Load(&fGbuf[0],1024);
    
	
	 // if(RoomHeader.lBg3& 0xF0 )	DrawBackGround(&ThisBackground.BImage); 
	
     ThisBackground.BImage.Blit(bbMap.DC(),0,0,Width ,Width,0,0);
 
	if(RoomHeader.bBg2){
		if(ThisBackLayer.Dirty){
		    ThisBackLayer.BImage.Create(ThisBackLayer.X*16,ThisBackLayer.Y*16);
            ThisBackLayer.BImage.SetPalette(&PcPalMem[0]);
			DrawLayer(&ThisBackLayer, RoomHeader.bBg2);//Backlayer
		    ThisBackLayer.Dirty=0;
	    } 

	 ThisBackLayer.BImage.TransBlit(bbMap.DC(),0,0,Width, Height,0,0);
	}
	
	if(RoomHeader.bBg1){
		if(ThisLevelData.Dirty){
		    ThisLevelData.BImage.Create(ThisLevelData.X*16,ThisLevelData.Y*16);
            ThisLevelData.BImage.SetPalette(&PcPalMem[0]);	
			DrawLayer(&ThisLevelData, RoomHeader.bBg1);//Level
			ThisLevelData.Dirty=0;
			
		}
	ThisLevelData.BImage.TransBlit(bbMap.DC(),0,0,Width, Height,0,0);
	}
	
				
				
	

	if(RoomHeader.bBg0){
		if(ThisForeground.Dirty){
		  ThisForeground.BImage.Create(ThisForeground.X*16,ThisForeground.Y*16);
          ThisForeground.BImage.SetPalette(&PcPalMem[0]);
		  DrawLayer(&ThisForeground, RoomHeader.bBg0);//Foreground
		  ThisForeground.Dirty=0;
	   	}

       // if(chkDoTrans.value() == 1)
		// ThisForeground.BImage.AlphaBlit(bbMap.DC(),0,0,Width, Height,0,0,255);
	   // else
		 ThisForeground.BImage.TransBlit(bbMap.DC(),0,0,Width, Height,0,0);
	    
	}
	
	  
	
	
	
	
	//imgMap.TransBlit(bbMap.DC(),0,0,Width, Height,0,0);
	//imgMap.Blit(bbMap.DC(),0,0,Width, Height,0,0);
////	ShowSprites((u8)Combos[cSpriteSet].GetListIndex());
	//DisplayDoors();
//	if(ViewClip.value()   ==1)DrawClipIdent();
//	if(ScrollCheck.value()==1)DrawScrolls  (cboScroll.GetListIndex());
	
	InvalidateRect(MapWindow.hwndMap,0,1);
     
	//SendMessage(hwndMap,WM_SIZE,1,0);

	return 0;
}
int WriteLayer(nRoomBuff* Room, long size){
            fputc( Room->X, GBA.ROM);
            fputc( Room->Y,GBA.ROM);
            fwrite(GBA.CompBuffer, sizeof(char), size, GBA.ROM);

	return 0;
}
int CompLayer(nRoomBuff* Room, unsigned long* pnt){
	 long            offset = 0;

   long            LayerSize;
		 memset(GBA.CompBuffer, 0, 64691);
         LayerSize = GBA.compress(GBA.CompBuffer, (u8*)Room->TileBuf2D, (Room->X * Room->Y) * 2);
         offset = GBA.FindFreeSpace(LayerSize + 4, 0xFF);
         if (offset != 0)
         {

            *pnt = 0x8000000 + offset;

            fseek(GBA.ROM, offset, SEEK_SET);
         
			WriteLayer(Room, LayerSize);
         } else
         {
           MessageBox(NULL,"Could not save.","None",MB_OK);
         }


return 0;
}
int clsGameClass::Save(){



   // Save only if it's RLE compression otherwise FUCK IT

      if (RoomHeader.bBg0 == 16)
      {
          CompLayer(&ThisForeground,&RoomHeader.lForeground);
          GBA.Reopen();
      }
      if (RoomHeader.bBg1 == 16)
      {
		  CompLayer(&ThisLevelData, &RoomHeader.lLevelData);
          GBA.Reopen();
      }
      if (RoomHeader.bBg2 == 16)
      {
		  CompLayer(&ThisBackLayer, &RoomHeader.lBackLayer);
          GBA.Reopen();
      }
         CompLayer(&ThisClipData, &RoomHeader.lClipData);
	     GBA.Reopen();
	 SaveHeader();
      
   
   return 0;


}

int clsGameClass::SaveHeader(){
    curRoomOffset= RoomOffsets[curArea]+(curRoom*0x3C)-0x8000000;
	//We'll do some invalid testing here I guess.
	GBA.Seek(curRoomOffset);

	fwrite(&RoomHeader.bTileset,sizeof(u8),1,GBA.ROM);
	fwrite(&RoomHeader.bBg0,sizeof(u8),1,GBA.ROM);
	fwrite(&RoomHeader.bBg1,sizeof(u8),1,GBA.ROM);
	fwrite(&RoomHeader.bBg2,sizeof(u8),1,GBA.ROM);
	fwrite(&RoomHeader.lBg3,sizeof(u32),1,GBA.ROM);
	fwrite(&RoomHeader.lForeground,sizeof(u32),1,GBA.ROM); 
	if(RoomHeader.lForeground<0x8000000) return -1;
	fwrite(&RoomHeader.lLevelData,sizeof(u32),1,GBA.ROM);
	if(RoomHeader.lLevelData<0x8000000) return -1;
	fwrite(&RoomHeader.lBackLayer,sizeof(u32),1,GBA.ROM);
	if(RoomHeader.lBackLayer<0x8000000) return -1;
	fwrite(&RoomHeader.lClipData,sizeof(u32),1,GBA.ROM); 
	if(RoomHeader.lClipData<0x8000000) return -1;
	fwrite(&RoomHeader.lBackgroundTSA,sizeof(u32),1,GBA.ROM);
	if(RoomHeader.lBackgroundTSA<0x8000000) return -1;
	fwrite(&RoomHeader.bUnknown1,sizeof(u8),1,GBA.ROM);
	fwrite(&RoomHeader.TransForeground,sizeof(u8),1,GBA.ROM);
	fwrite(&RoomHeader.iSep1,sizeof(u16),1,GBA.ROM);
	fwrite(&RoomHeader.lSpritePointer,sizeof(u32),1,GBA.ROM); 
	if(RoomHeader.lSpritePointer<0x8000000) return -1;
	fwrite(&RoomHeader.bSpriteIndex1,sizeof(u8),1,GBA.ROM);
	fwrite(&RoomHeader.bEventSwitch,sizeof(u8),1,GBA.ROM);
	fwrite(&RoomHeader.iSep2,sizeof(u16),1,GBA.ROM);
	fwrite(&RoomHeader.lSpritePointer2,sizeof(u32),1,GBA.ROM);
	if(RoomHeader.lSpritePointer2<0x8000000) return -1;
	fwrite(&RoomHeader.bSpriteIndex2,sizeof(u8),1,GBA.ROM);
	fwrite(&RoomHeader.bEventSwitch2,sizeof(u8),1,GBA.ROM);
	fwrite(&RoomHeader.iSeperator,sizeof(u16),1,GBA.ROM);
	fwrite(&RoomHeader.lSpritePointer3,sizeof(u32),1,GBA.ROM); 
	if(RoomHeader.lSpritePointer3<0x8000000) return -1;
	fwrite(&RoomHeader.bSpriteIndex3,sizeof(u8),1,GBA.ROM);
	fwrite(&RoomHeader.bMiniMapRoomX,sizeof(u8),1,GBA.ROM);
	fwrite(&RoomHeader.bMiniMapRoomY,sizeof(u8),1,GBA.ROM);
	fwrite(&RoomHeader.bEffect,sizeof(u8),1,GBA.ROM);
	fwrite(&RoomHeader.bSceneryYPos,sizeof(u8),1,GBA.ROM);
	fwrite(&RoomHeader.bNothing,sizeof(u8),1,GBA.ROM);
	fwrite(&RoomHeader.bMusic,sizeof(u8),1,GBA.ROM);
	fwrite(&RoomHeader.blank,sizeof(u8),1,GBA.ROM);

    
     return curRoomOffset;

}
